package cn.edu.hunau.controller;


import cn.edu.hunau.pojo.Car;
import cn.edu.hunau.pojo.Product;
import cn.edu.hunau.service.ProductService;
import cn.edu.hunau.service.impl.ProductServiceImpl;
import cn.edu.hunau.utils.JsonUtils;
import cn.edu.hunau.utils.R;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 苏佳
 * @Date 2022 11 07 19 20
 **/
@WebServlet("*.car")
public class CarController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        String matchValue = req.getHttpServletMapping().getMatchValue();
        HttpSession session = req.getSession();

        ProductService productService = new ProductServiceImpl();

        if("add".equals(matchValue)){
            //添加购物车
            String pidStr = req.getParameter("pid");
            String qtyStr = req.getParameter("qty");
            String sizeStr = req.getParameter("size");
            String shoppingPriceStr = req.getParameter("shoppingPrice");

            int pid = Integer.parseInt(pidStr.trim());
            int qty = Integer.parseInt(qtyStr.trim());
            int size = Integer.parseInt(sizeStr.trim());
            double shoppingPrice = Double.parseDouble(shoppingPriceStr.trim());

            //实例化一个购物车条目
            Product product = productService.findProductById(pid);
            //进行状态管理  从session中获取购物车的数据
            Car car = (Car)session.getAttribute("car");
            if(car == null){
                // 实例化car对象
                car = new Car();
                // 将购物车储存到session中
                session.setAttribute("car",car);
            }
            //将商品添加到购物车中
            car.add(product,qty,size,shoppingPrice);

            out.println(JsonUtils.objectToJson(R.ok().message("添加成功!")));
        }else if ("show".equals(matchValue)) {
            Car car = (Car)session.getAttribute("car");
            out.println(JsonUtils.objectToJson(R.ok().data(car)));
        }else if ("clear".equals(matchValue)){
            session.removeAttribute("car");
            out.println(JsonUtils.objectToJson(R.ok().message("清空成功!")));
        }else if ("remove".equals(matchValue)){
            String pidStr = req.getParameter("pid");
            String sizeStr = req.getParameter("size");

            int pid = Integer.parseInt(pidStr.trim());
            int size = Integer.parseInt(sizeStr.trim());

            Car car = (Car)session.getAttribute("car");
            car.remove(pid,size);
            out.println(JsonUtils.objectToJson(R.ok().message("删除成功!")));
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
